package com.example.lvsuo.controller.cxg;


import com.example.lvsuo.entity.SystemRole;
import com.example.lvsuo.entity.cxg.FlowNode;
import com.example.lvsuo.entity.cxg.vo.FlowDataVo;
import com.example.lvsuo.entity.cxg.vo.FlowNodeVo;
import com.example.lvsuo.modal.Result;
import com.example.lvsuo.service.ISystemRoleService;
import com.example.lvsuo.service.cxg.FlowDataService;
import com.example.lvsuo.service.cxg.IFlowLineService;
import com.example.lvsuo.service.cxg.IFlowNameService;
import com.example.lvsuo.service.cxg.IFlowNodeService;
import org.apache.commons.collections.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 心心念念
 * @since 2021-03-30
 *
 * 流程审批人 Controller
 */
@RestController
@RequestMapping("/flownodes")
public class FlowNodeController {

    @Autowired
    IFlowNodeService iFlowNodeService;
    @Autowired
    ISystemRoleService iSystemRoleService;
    @Autowired
    FlowDataService flowDataService;

    /**
     * 查询流程审批人信息
     * @return
     */
    @GetMapping
    public Result queryFlowNode( FlowNodeVo flowNodeVo){
        List<FlowNodeVo> flownodes = iFlowNodeService.queryFlowNode(flowNodeVo);
        return Result.back("ok","20000",flownodes);
    }

    @GetMapping(params = {"chaxun=2"})
    public Result queryRole(){
        List<SystemRole> list = iSystemRoleService.queryRole();
        return  Result.back("ok","20000",list);
    }

    @GetMapping(params = {"chaxun=3"})
    public Result queryByid(FlowNode flowNode){
        System.out.println("updateByid"+flowNode);
        FlowNode flowNode1 = iFlowNodeService.queryByid(flowNode);
        return  Result.back("ok","20000",flowNode1);
    }

    /**
     * 添加流程审批人
     * @param flowNode
     * @return
     */
    @PostMapping
    public Result addFlowNode(@RequestBody FlowNode flowNode){
//        System.out.println("add"+flowNode);
         iFlowNodeService.addFlowNode(flowNode);
        return Result.back("ok","20000");
    }

    @DeleteMapping("/{id}")
    public Result delFlowNode(@PathVariable String id){

        // 先查询当前审批节点是否有审批
        List<FlowDataVo> flowDataVos = flowDataService.queryFlowDataBynowId(id);

//        System.out.println("当前节点的业务:"+flowDataVos);

        if(flowDataVos.size() == 0){
            iFlowNodeService.delFlowNode(id);
            return Result.back("ok","20000","ok");
        }

        return Result.back("ok","20000","error");
    }


@PutMapping
public Result updateFlowNode(@RequestBody FlowNode flowNode){
//    System.out.println("update"+flowNode);
    iFlowNodeService.updateFlowNode(flowNode);
    return Result.back("ok","20000");
}

}

